﻿using MediatR;
using SystemHYS.API.Application.command.GualityGroupCommand;
using SystemHYS.Domian.DTO;
using SystemHYS.Domian;
using SystemHYS.Infrstructre.Interface;

namespace SystemHYS.API.Application.commandler.GualityGroupCommandler
{
    public class GualityGroupShowCommandler:IRequestHandler<GualityGroupShowCommand, PageDTO<GualityGroup>>
    {
        IGualityGroupRepository GroupRepository;
        public GualityGroupShowCommandler(IGualityGroupRepository GroupRepository)
        {
            this.GroupRepository = GroupRepository;
        }

        public async Task<PageDTO<GualityGroup>> Handle(GualityGroupShowCommand request, CancellationToken cancellationToken)
        {
            var list = from a in await GroupRepository.GetAllAsnc() where a.IsDel==false select a;
            if (request.gualityGroupName != null)
            {
                list=list.Where(x=>x.gualityGroupName .Contains(request.gualityGroupName));
            }
            if (request.gualityGroupNumbering != null)
            {
                list=list.Where(x=>x.gualityGroupNumbering.Contains(request.gualityGroupNumbering));
            }
            PageDTO<GualityGroup> page = new PageDTO<GualityGroup>();
            page.listcount = list.Count();
            page.pagecount = (int)Math.Ceiling(list.Count()*1.0 / request.pagesize);
            page.plist=list.OrderByDescending(x=>x.AddTime).Skip((request.pageindex-1)/request.pagesize).Take(request.pagesize).ToList();
            return page;

        }
    }
}
